package com.zzy.demo.common.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;

import com.zzy.cms.core.util.UUIDGenerator;
import com.zzy.cms.core.util.string.StringsUtil;

public class UploadFileUtils {
	
	
	
	
	/**
	 * 得到图片字节流 数组大小
	 */
	public static String createFileName(String originalFileName) throws Exception {
		String fileName=UUIDGenerator.nextId()+StringsUtil.UNDERLINE+originalFileName;
		return fileName;
	}
	
	/**
	 * 输入流转文件 
	 * @param stream
	 * @param targetPath
	 * @param targetFileName
	 * @return
	 */
	public static File inputStream2File(InputStream stream,String targetPath,String targetFileName){
		//创建父级目录
		File dir = new File(targetPath);
		if (!dir.exists() && dir.isDirectory()) {
			boolean flag = dir.mkdirs();
			if (!flag) {
				System.out.println("创建父级目录失败！");
			}
		}
		//上传文件
		File targetFile = new File(targetPath + File.separator + targetFileName);
		try {
			FileUtils.copyInputStreamToFile(stream, targetFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return targetFile;
	}
	
	

}
